#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018\9\5 0005 19:30
# @Author  : never mind
# @File    : move_in_policy.py

import copy
import utils_list
import constants as cons
import utils
import numpy as np

# 策略 一
def get_queue_position(fig_queue, occ):
    '''
    根据开销大小对要停放的位置进行排序之后，选取开销最小的位置
    :param fig_queue:
    :param occ:
    :return:
    '''
    for item in fig_queue:
        # importance = item[0]
        # cost = item[1]
        pos_out = [int(v) for v in item[2].split("_")]
        pos, out = pos_out[:2], pos_out[2:]
        # cost_posxy_outxy = item[1]
        # [outx, outy] = cost_posxy_outxy.split('_')[1:3]
        # outx, outy = int(outx), int(outy)
        # in_out = [outx, outy]
        in_out = pos
        if in_out not in occ:
            break
        if in_out in cons.pos_out or in_out in cons.pos_in:  # 若是出入口，继续选取
            continue
    # flag = np.random.randint(0, 2, 1)
    # flag = 'in' if flag == 0 else 'out'
    return in_out


# 策略 二
def get_random_position():
    '''
    获取随机停放车辆的坐标
    :return:
    '''
    while True:
        flag_rand = np.random.randint(0, 2, 1)[0]
        flag = ['in' if flag_rand == 0 else 'out'][0]
        pos = np.random.randint(1, cons.axis_num * cons.axis_alpha / 10.0, 2)
        if not check_in_out_pos(list(pos)):
            break
    return flag, list(pos)


# 策略 三
def get_handcraft_position():
    '''
    手动输入停放车辆位置的操作
    :return:
    '''
    in_out = input("请输入一个坐标，eg. in, 1, 2 ; out, 2, 3\n")
    in_out = in_out.strip().split(',')
    flag = in_out[0]
    in_out = [int(x) for x in list(in_out[1:3])]
    return flag, in_out
    pass

def get_trace_position():
    '''
    根据已经设定好的路径选取下一个停放的车辆
    :return:
    '''
    pass

def policy_two(init_position_copied, in_copied, occ):
    pass

def check_in_out_pos(in_out_pos):
    '''
    检查出入坐标是否是出入口
    :return:
    '''
    if in_out_pos in cons.pos_in or in_out_pos in cons.pos_out:
        return True
    return False

def get_cost(path, use_important_level=False):
    # 入车的开销：
    # 1、	将小车送到目标位置的距离、时间上的开销
    # 2、	将小车放下的开销	，也就是能量开销
    distance_cost, time_cost = utils.compute_distance_score(path, use_important_level=use_important_level)
    energy_cost = utils.computer_energy_score(path)
    total_cost = distance_cost + time_cost + energy_cost
    print('distance_cost: %s, time_cost: %s, energy_cost: %s, total cost: %s'
          % (distance_cost, time_cost, energy_cost, total_cost))
    return total_cost